﻿using System.Data.Entity;
using Microsoft.Practices.Unity;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PharmacyBussinesEntities;
using PharmacyRepositoy;

namespace PharmacyRepositoy
{
    public class CompraRepository:MasterRepository,ICompraRepository
    {
       
        public IEnumerable<PharmacyBussinesEntities.Compra> GetComprasByCriterio(string criterio)
        {
            var query = from p in Context.Compras
                           .Include("DetalleCompra")
                        select p;
            if (string.IsNullOrEmpty(criterio))
            {
                query = from p in query
                        where p.DetalleCompra.ToString().Contains(criterio.ToUpper())
                        select p;

            }
            return query;
        }

        public Compra GetCompraById(int id)
        {
            var query = from p in Context.Compras
                        select p;
            if (id != null)
            {
                query = from p in query
                        where p.Id.Equals(id)
                        select p;
            }
            return query.SingleOrDefault();
        }

        public void AddCompra(Compra compra)
        {
            Context.Compras.Add(compra);
            Context.SaveChanges();
        }

        public void UpdateCompra(Compra compra)
        {

            Context.Compras.Attach(compra);
            Context.Entry(compra).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveCompra(int id)
        {
            var exist = Context.Compras.Find(id);
            if (exist != null)
            {
                Context.Compras.Remove(exist);
                Context.SaveChanges();
            }
        }
    }
}
